$(function(){
     
Date.prototype.format = function(format){ 
		var o = { 
		"M+" : this.getMonth()+1, //month 
		"d+" : this.getDate() //day
		} 

		if(/(y+)/.test(format)) { 
			format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
		} 

		for(var k in o) { 
			if(new RegExp("("+ k +")").test(format)) { 
				format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); 
			} 
		} 
		return format; 
	}
        var getHashData = function(hash){
            if(!hash) return false;
            var data = {};

            var optArr = hash.split("&");
            var x = 0;
            for(var a in optArr){
                var s = optArr[a].split("=");
                data[s[0]] = s[1];
            }

            return data;
        };

		var tsweek = new Date();
		var tsa=tsweek.format("yyyy-MM-dd");//今天
		tsweek.setDate(tsweek.getDate() - 1);
		var yesterday=tsweek.format("yyyy-MM-dd");//昨天
		tsweek.setDate(tsweek.getDate() - tsweek.getDay() + 1);
		var ts= tsweek.format("yyyy-MM-dd");
		tsweek.setDate(tsweek.getDate()-7);
		var pre= tsweek.format("yyyy-MM-dd");
		tsweek.setDate(tsweek.getDate()+6);
		var prea= tsweek.format("yyyy-MM-dd");
        tsweek.setDate(tsweek.getDate()-5);
        var seven=tsweek.format("yyyy-MM-dd");
        //console.log(a);
        if(location.hash.length>10){
            var be = getHashData(location.hash.substring(1))
            $("#startDate").val(be.btime);
            $("#endDate").val(be.etime);
        }else{
            $("#startDate").val(seven);
            $("#endDate").val(yesterday);
            var starttime= seven;
            var endtime  =yesterday;
            changeType (starttime,endtime);
        }
		var myDate = new Date();
		var year = myDate.getFullYear();
		var month = myDate.getMonth()+1;
		if (month<10){
			month = "0"+month;
		}
		var firstDay =year+"-"+month+"-"+"01";
		month=month-1;
		var prfirstDay =year+"-"+month+"-"+"01";
		 myDate = new Date(year,month,0);
		var lastDay = year+"-"+month+"-"+myDate.getDate();
		$(".yesterday").click(function(){
			$(this).siblings().removeClass("active");
			$(this).addClass("active");
			$("#startDate").val(yesterday);
			$("#endDate").val(yesterday);
            var starttime = yesterday;
            var endtime   = yesterday;
            changeType (starttime,endtime);
		});//昨天
	

		$(".tsmonth").click(function(){
			$(this).siblings().removeClass("active");
			$(this).addClass("active");
			$("#startDate").val(firstDay);
			$("#endDate").val(tsa);
            var starttime = firstDay;
            var endtime   = tsa;
            changeType (starttime,endtime);
		});//本月
		$(".prmonth").click(function(){
			$(this).siblings().removeClass("active");
			$(this).addClass("active");
			$("#startDate").val(prfirstDay);
			$("#endDate").val(lastDay);
            var starttime = prfirstDay;
            var endtime   = lastDay;
            changeType (starttime,endtime);
		});//上月

		$(".tswk").click(function(){
			$(this).siblings().removeClass("active");
			$(this).addClass("active");
            $("#startDate").val(seven);
            $("#endDate").val(yesterday);
            var starttime= seven;
            var endtime  =yesterday;
            changeType (starttime,endtime);
		});//本周
		$(".prwk").click(function(){
			$(this).siblings().removeClass("active");
			$(this).addClass("active");
			$("#startDate").val(pre);
			$("#endDate").val(prea);
            var starttime = pre;
            var endtime   = prea;

            changeType (starttime,endtime);
		});//上周
		$(".clear").click(function(){
			$(this).siblings().removeClass("active");
			//$(this).addClass("active");
			$("#startDate").val("");
			$("#endDate").val("");
		});
		$("#startDate,#endDate").click(function(){
			$(".datemsg li").removeClass("active");
		});
 });  
   
   
   
   
   
   
   
   
   
   
   
   
   
   